﻿///***************************************************************
/// 시  스  템 : Updms.Crd
/// 단위시스템 : CRD
/// 계      층 : Presentation Layer
/// 프로그램명 : Crd2014r
/// 파  일  명 : Crd2014r.aspx.cs
/// 설      명 : 결제총액챠트(일별)
///***************************************************************
/// 수정ID 일자       이름     내용
///---------------------------------------------------------------
/// CH001  2008-07-21 송시명   최초 작성
///***************************************************************
using System;
using System.Data;
using System.Web.UI;

using SMFramework.Data;
using SMFramework.Utility;

using Base.Dac;
using Updms.Crd.Exe;


namespace Updms.Crd
{
	public partial class Crd2014r : Updms.Common.BaseClass.PageBase
	{
		#region 전역영역

		public static readonly string PID = "Crd2014r";
		public static readonly string gs_path_info = "/updms/Crd/" + PID + ".aspx";
		
		Global global = new Global();

		//검색
		protected string kw1 = "";	//카드
		protected string kw2 = "";	//검색시작일
		protected string kw3 = "";	//검색종료일
		protected string kw4 = "";	//CRD
		protected string kw5 = "";	//C

		//메뉴 선택값
		protected string gs_sm = "";

		// 페이지모드
		private string gs_page_mode = "";

		#endregion

		#region Page_Load, OnPageLoad

		protected void Page_Load(object sender, EventArgs e)
		{
			SetInit();
			if (!Page.IsPostBack)
			{
				// 최초 화면 로딩시 처리해야 할 부분
				OnPageLoad();
			}
		}

		// OnPageLoad
		private void OnPageLoad()
		{
			SetLiterals(global.GetLabelInfoByPgmId(PID, LANG_SET));
			SetClientScript();
			SetCardInfo();
			SetSearchParameter();
		}

		#endregion

		#region InitializeComponent

		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);
			this.PreRender += new System.EventHandler(this.Page_PreRender);
		}

		#endregion		
		
		#region SetClientScript
		
		private void SetClientScript()
		{
			//달력콘트롤 설정
			ce_search_biz_from_dt.SelectedDate = DateTime.Now.AddMonths(-1);//1개월전
			ce_search_biz_to_dt.SelectedDate = DateTime.Now.AddMonths(0);//현재

			txt_search_biz_from_dt.Attributes.Add("onkeypress", "enterSearchOnPage(event)");
			txt_search_biz_to_dt.Attributes.Add("onkeypress", "enterSearchOnPage(event)");
		}
		
		#endregion

		#region SetInit, SetSearchParameter, ShowKeywordLabel

		//초기값설정
		private void SetInit()
		{
			lbl_search_flag.Text = "INIT";		

			gs_sm = (Request.QueryString["sm"] == null || Request.QueryString["sm"].Trim().Length != 7)
					? global.GetDirDepth(PID) : Request.QueryString["sm"].ToString();
		}

		//검색파라미터
		private void SetSearchParameter()
		{
			string ls_checked_data = "";
			switch (lbl_search_flag.Text.Trim().ToUpper())
			{
				case "INIT":
					if (String.IsNullOrEmpty(Request.QueryString["kw1"]))	// url에 값이 없으면
					{
						for (int i = 0; i < cbl_search_card_cd.Items.Count; i++)
						{
							if (cbl_search_card_cd.Items[i].Selected == true)
							{
								ls_checked_data += cbl_search_card_cd.Items[i].Value + ",";
							}
						}
						kw1 = ls_checked_data.Substring(0, ls_checked_data.LastIndexOf(","));
					}
					else
					{
						kw1 = Server.UrlDecode(Request.QueryString["kw1"]);
						//kw1의 내용을 체크박스리스트에 바인딩
					}
					
					if (String.IsNullOrEmpty(Request.QueryString["kw2"]))	// url에 값이 없으면
					{
						if (String.IsNullOrEmpty(txt_search_biz_from_dt.Text))
						{
							kw2 = DateUtil.GetMonthEx(-1);	// 오늘부터 1개월전
							txt_search_biz_from_dt.Text = kw2;
						}
						else
						{
							kw2 = txt_search_biz_from_dt.Text.Trim();
						}
					}
					else
					{
						kw2 = Server.UrlDecode(Request.QueryString["kw2"]);
						txt_search_biz_from_dt.Text = kw2;
					}

					if (String.IsNullOrEmpty(Request.QueryString["kw3"]))	// url에 값이 없으면
					{
						if (String.IsNullOrEmpty(txt_search_biz_to_dt.Text))
						{
							kw3 = DateUtil.GetMonthEx(0);	// 오늘
							txt_search_biz_to_dt.Text = kw3;
						}
						else
						{
							kw3 = txt_search_biz_to_dt.Text.Trim();
						}
					}
					else
					{
						kw3 = Server.UrlDecode(Request.QueryString["kw3"]);
						txt_search_biz_to_dt.Text = kw3;
					}
					break;
				case "SEARCH":
					for (int i = 0; i < cbl_search_card_cd.Items.Count; i++)
					{
						if (cbl_search_card_cd.Items[i].Selected == true)
						{
							ls_checked_data += cbl_search_card_cd.Items[i].Value + ",";
						}
					}
					kw1 = ls_checked_data.Substring(0, ls_checked_data.LastIndexOf(","));
					kw2 = String.IsNullOrEmpty(txt_search_biz_from_dt.Text) ? DateUtil.GetMonthEx(-1) : txt_search_biz_from_dt.Text.Trim();	// 오늘부터 1개월전
					kw3 = String.IsNullOrEmpty(txt_search_biz_to_dt.Text) ? DateUtil.GetMonthEx(0) : txt_search_biz_to_dt.Text.Trim();	// 오늘
					break;
			}

			kw4 = cb_support_yn.Checked ? "CRD" : "";
			kw5 = cb_support_yn.Checked ? "AG" : "";
		}
		
		#endregion

		#region 마스터 Method (SetCardInfo, SetYearMonth)

		//카드정보
		private void SetCardInfo()
		{
			DataSet lo_ds = new DataSet();
			Crd1010m_NT _Crd1010m_NT = new Crd1010m_NT();

			lo_ds.Merge(_Crd1010m_NT.GetCardInfoList("A"));

			cbl_search_card_cd.Items.Clear();
			cbl_search_card_cd.DataSource = lo_ds;
			cbl_search_card_cd.DataMember = "UPDMS_CRD_INFO";
			cbl_search_card_cd.DataTextField = "Card_Nm_Color";
			cbl_search_card_cd.DataValueField = "Card_Cd";
			cbl_search_card_cd.DataBind();

			if (cbl_search_card_cd.Items.Count > 0)
			{
				cbl_search_card_cd.Items[0].Selected = true;
			}
		}
		
		// 검색조건의 년월설정 - 기간지정
		private void SetYearMonth(int li_term)
		{
			txt_search_biz_from_dt.Text = DateUtil.GetMonthEx(li_term);
			txt_search_biz_to_dt.Text = DateUtil.GetMonthEx(0);
		}

		#endregion

		#region 챠트 (DrawChartLine, DrawDailyWeekdayChart, SearchProcess)

		//조회
		private void DrawChartLine(DataPack lo_dp)
		{
			DataSet lo_ds = new DataSet();

			//초기화
			chart_mline_total.Data.Clear();

			using (Crd2014r_NT _Crd2014r_NT = new Crd2014r_NT())
			{
				lo_ds.Merge(_Crd2014r_NT.GetDailyTotalAmt(lo_dp));
				int li_count = DataSetUtil.GetRowCount(lo_ds, "UPDMS_CRD_REPORT");

				for (int i = 0; i < li_count; i++)
				{
					//수동으로 데이터 바인딩
					chart_mline_total.Data[0, i] = DataSetUtil.GetFldInt(lo_ds, "UPDMS_CRD_REPORT", i, "Total_Amt");

					//X축 라벨링
					chart_mline_total.AxisX.Labels[i] = "'" + DataSetUtil.GetFldStr(lo_ds, "UPDMS_CRD_REPORT", i, "Biz_Dt").Substring(2, 8).Replace("-", ".") + "(" + DataSetUtil.GetFldStr(lo_ds, "UPDMS_CRD_REPORT", i, "Day_Of_Week") + ")";

					chart_mline_total.Points[0, i].PointLabels.Visible = (chart_mline_total.Data[0, i] > 0);
				}
			}
		}

		//요일방사챠트
		private void DrawDailyWeekdayChart(DataPack lo_dp)
		{
			DataSet lo_ds = new DataSet();
			lo_dp.AddProperty("ls_lang_set", typeof(string), LANG_SET);
			
			//초기화
			chart_daily_weekday.Data.Clear();

			using (Crd2014r_NT _Crd2014r_NT = new Crd2014r_NT())
			{
				lo_ds.Merge(_Crd2014r_NT.GetDailyWeekdayAmt(lo_dp));
				int li_count = DataSetUtil.GetRowCount(lo_ds, "UPDMS_CRD_REPORT");

				//X축 라벨링
				for (int i = 0; i < li_count; i++)
				{
					//수동으로 데이터 바인딩
					chart_daily_weekday.Data[0, i] = DataSetUtil.GetFldInt(lo_ds, "UPDMS_CRD_REPORT", i, "Total_Amt");
					chart_daily_weekday.AxisX.Labels[i] = DataSetUtil.GetFldStr(lo_ds, "UPDMS_CRD_REPORT", i, "Week_Day");
				}
			}
		}

		//검색
		private void SearchProcess()
		{
			DataPack lo_dp = new DataPack();

			gs_page_mode = "R";
			lbl_search_flag.Text = "SEARCH";
			SetSearchParameter();

			//검색
			lo_dp.AddProperty("ls_card_cd", typeof(string), kw1);
			lo_dp.AddProperty("ls_from_dt", typeof(string), kw2);
			lo_dp.AddProperty("ls_to_dt", typeof(string), kw3);
			lo_dp.AddProperty("ls_exc_sys_id", typeof(string), kw4);
			lo_dp.AddProperty("ls_exc_prefix", typeof(string), kw5);

			DrawChartLine(lo_dp);
			DrawDailyWeekdayChart(lo_dp);
		}

		#endregion

		#region Page_PreRender, ChangePageMode

		// Page_PreRender
		private void Page_PreRender(object sender, System.EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				using (AccessCheck access = new AccessCheck())
				{
					//접근권한 체크
					if (!access.CheckPgmAccessGrant(PID, USER_ID, ACCESS_LOG_INFO))
					{
						Response.Redirect(URL_NO_AUTHORITY_REDIRECT_BASE);	//사용자의 권한은 요청하신 페이지를 볼 수 없습니다
					}
				}
			}
			ChangePageMode();
		}

		// ChangePageMode
		private void ChangePageMode()
		{
			switch (gs_page_mode)
			{
				case "":		// 처음 페이지로드시
					table_chart.Visible = false;	// 챠트
					break;
				case "R":	// 검색버튼 클릭시
					table_chart.Visible = true;	// 챠트
					break;
			}
		}

		#endregion
		
		#region 버튼이벤트

		//1개월
		protected void sbtn_1_m_Click(object sender, EventArgs e)
		{
			SetYearMonth(-1);
		}

		//3개월
		protected void sbtn_3_m_Click(object sender, EventArgs e)
		{
			SetYearMonth(-3);
		}
		
		//6개월
		protected void sbtn_6_m_Click(object sender, EventArgs e)
		{
			SetYearMonth(-6);
		}

		//12개월
		protected void sbtn_12_m_Click(object sender, EventArgs e)
		{
			SetYearMonth(-12);
		}

		//전체
		protected void sbtn_all_m_Click(object sender, EventArgs e)
		{
			SetSearchParameter();
			
			DataPack lo_dp = new DataPack();
			DataSet lo_ds = new DataSet();

			lo_dp.AddProperty("ls_card_cd", typeof(string), kw1);

			using (Crd1011m_NT _Crd1011m_NT = new Crd1011m_NT())
			{
				lo_ds.Merge(_Crd1011m_NT.GetMinNMaxBizDt(lo_dp));
				txt_search_biz_from_dt.Text = DataSetUtil.GetFldStr(lo_ds, "UPDMS_CRD_PAYMNT_DT", "Min_Dt");
				txt_search_biz_to_dt.Text = DataSetUtil.GetFldStr(lo_ds, "UPDMS_CRD_PAYMNT_DT", "Max_Dt");
			}
		}

		//챠트
		protected void ibtn_chart_Click(object sender, EventArgs e)
		{
			SearchProcess();
		}

		#endregion	


	}
}
